package kr.co.iniline.rumkb.server;

/**
 * Index : 7bit(64,0x40)
 * Color : 5-6bit(48,0x30)
 * Num : 1-4bit(15,0x0F)
 * @author ktjohn
 *
 */
public class RmkbUtil {
	public static final int EMPTY = -1;
	
	enum KubCol {
		RED,
		BLUE,
		YELLOW,
		BLACK
	}
	
	public static final int RED = 0;
	public static final int BLUE = 1;
	public static final int YELLOW = 2;
	public static final int BLACK = 3;
	
	public static byte getColor(int kubData) {
		return getColor((byte)kubData);
	}
	
	public static byte getColor(byte kubData) {
		return (byte) (  (kubData&0x30) >> 4  );
	}

	public static byte getKubNum(int kubData) {
		return getKubNum((byte)kubData);
	}
	
	public static byte getKubNum(byte kubData) {
		int num = kubData & 0xF;
		return (byte)num;
	}
	
	public static boolean isJoker(byte kubData) {
		if( kubData == 0 ) return true;
		return false;
	}
	
	public static String toKubString(byte kubData) {
		if( kubData == 0 ) return "X(J)";
		byte col = getColor(kubData);
		StringBuilder sb = new StringBuilder();
		KubCol cols[] = KubCol.values();
		for (int i = 0; i < cols.length; i++) {
			if( cols[i].ordinal() == col ) {
				sb.append( cols[i].toString() );
			}
		}
		sb.append("(");
		sb.append( getKubNum(kubData) );
		sb.append(")");
		return sb.toString();
	}
	
	public static int toKubData(int col, int num) {
		return (col << 4 | num);
	}
	
	public static String toKubString(int kubData) {
		return toKubString( (byte)kubData );
	}
	
	public static void main(String args[]) {
		System.out.println( toKubString( toKubData(BLUE,1) ) );
		System.out.println( toKubString( 18 ) );
		System.out.println( toKubString( 5 ) );
		System.out.println( toKubString((byte)17) );
		System.out.println( toKubString( -1 ) );
		System.out.println( getColor( -1 ) );
		System.out.println( (15<<4) );
		System.out.println( toKubData(BLACK,13) );
		System.out.println( toKubData(BLACK, 13) );
		System.out.println( toKubString(61) );
		System.out.println( toKubString(125) );
		System.out.println( getColor(125) );
	}
}
